import sys
import os
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/'))
import numpy as np
import qiskit
provider = qiskit.IBMQ.load_account()
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit.algorithms import QAOA
from shared.QiskitMaxcut import *
from ibm.ibm_parameters import *
from matplotlib import pyplot as plt
%matplotlib inline
from ibm_landscape_processes import *
ibmqfactory.load_account:WARNING:2021-08-20 18:30:22,239: Credentials are already in use. The existing account in the session will be replaced.
%load_ext autoreload
%autoreload 2
# ---- Define graph and MaxCut ----
graph = generate_butterfly_graph(with_weights=False)
max_cut = Maxcut(graph)
max_cut_qubo = max_cut.to_qubo()
max_cut.draw()
# ---- Define step size and gamma, beta values ----
step_size = 0.1
a_gamma = np.arange(0, np.pi, step_size)
b_beta = np.arange(0, np.pi, step_size)
gammas, betas = np.meshgrid(a_gamma, b_beta)
# ---- execute for all values QAOA and get result matrix ----
landscape = run_all(gammas, betas, max_cut)
Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10 Row 11 Row 12 Row 13 Row 14 Row 15 Row 16 Row 17 Row 18 Row 19 Row 20 Row 21 Row 22 Row 23 Row 24 Row 25 Row 26 Row 27 Row 28 Row 29 Row 30 Row 31
print(landscape)
plt.matshow(landscape)
plt.show()
[[-2.98950005 -2.99825001 -2.98300004 ... -3.01149988 -2.99799991 -3.00200009] [-2.9914999 -2.88474989 -2.78999996 ... -3.24950004 -3.14875007 -3.04025006] [-2.98049998 -2.80349994 -2.55800009 ... -3.46125007 -3.30649996 -3.10349989] ... [-2.99000001 -3.22900009 -3.4602499 ... -2.38875008 -2.59925008 -2.91175008] [-2.98674989 -3.16149998 -3.30075002 ... -2.66100001 -2.76574993 -2.91849995] [-3.02874994 -3.05725002 -3.08675003 ... -2.90324998 -2.94199991 -2.95274997]]
# Mean of landscape
np.mean(landscape)
-2.6386364763602614
# Minimium
np.min(landscape)
-3.9277501106262207
# Display Coordinates of Minimum
np.unravel_index(np.argmin(landscape), landscape.shape)
(26, 19)
# Gamma and beta value of Minimium
gamma, beta = np.unravel_index(np.argmin(landscape), landscape.shape)
opt_gamma = gamma * step_size
opt_beta = beta * step_size
print(f"Opt.Gamma: {opt_gamma}, Opt.Beta: {opt_beta}")
Opt.Gamma: 2.6, Opt.Beta: 1.9000000000000001
# Save result matrix
with open('landscape_simulator_butterfly_no_weights_results.npy', 'wb') as f:
np.save(f, landscape)
import plotly.graph_objects as go
# Plot landscape in 3D
a_gamma = np.arange(0, np.pi, step_size)
b_beta = np.arange(0, np.pi, step_size)
fig = go.Figure(data=go.Surface(z=landscape, x=a_gamma, y=b_beta))
fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor='limegreen', project_z=True))
fig.update_layout(title="QAOA MaxCut", scene=dict(
xaxis_title="gamma",
yaxis_title="beta",
zaxis_title="mean"
))
# Plot Heatmap
fig = go.Figure(data=go.Heatmap(z=landscape, x=b_beta, y=a_gamma, type = 'heatmap', colorscale = 'viridis'))
# Update Layout
fig.update_layout(title="QAOA MaxCut", width=700, height=700, xaxis_title="beta", yaxis_title="gamma")
# Display Global Minimium
fig.add_trace(
go.Scatter(mode="markers", x=[opt_beta], y=[opt_gamma], marker_symbol=[204], text = [landscape[gamma,beta]],
marker_color="red", hovertemplate="x: %{x}<br>y: %{y}<br> z: %{text:.2f}<extra></extra>",
marker_line_width=1, marker_size=16))
# Display Optimizer Results
# Display path
#fig.add_trace(
# go.Scatter(mode="lines", x=gammas, y=betas, marker_symbol=[200],
# marker_color="white", marker_line_width=1, marker_size=8)
#)
# Display start point
#fig.add_trace(
# go.Scatter(mode="markers", x=[gammas[0]], y=[betas[0]], marker_symbol=[204],
# marker_color="gray",
# marker_line_width=1, marker_size=16))
# Display end point
#fig.add_trace(
# go.Scatter(mode="markers", x=[gammas[-1]], y=[betas[-1]], marker_symbol=[204],
# marker_color="green",
# marker_line_width=1, marker_size=16))
# Plot Optimizer History
#fig = go.Figure(data=go.Scatter(x=counts, y=values))
#fig.update_layout(xaxis_title="Evaluation Counts", yaxis_title="Evaluated Mean", title="Optimizer")
#fig.show()